
  public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
  class Solution {
      public ListNode reverseBetween(ListNode head, int left, int right) {
          if(head == null || head.next == null){
              return head;
          }
          ListNode cur = head;
          int len = 0;
          while(cur != null){
              cur = cur.next;
              len++;
          }
          if(left < 1 || right > len){
              return null;
          }
          if(right == left){
              return head;
          }
          left--;
          ListNode leftN = head;
          ListNode rightN = head;
          ListNode leftCur = leftN;
          while(left != 1 && left > 0){
              leftN = leftN.next;
              leftCur = leftN;
              left--;
          }
          leftN = leftCur.next;
          while(right != 1 && right >= 0){
              rightN = rightN.next;
              right--;
          }
          ListNode rightCur = rightN.next;
          if(left != 0){
              while(leftN != rightN){
                  ListNode leftNext = leftN.next;
                  leftN.next = rightCur;
                  rightCur = leftN;
                  leftN = leftNext;
              }
              rightN.next = rightCur;
              leftCur.next = leftN;
          }else{
              leftN = head;
              while(rightN != rightCur){
                  ListNode leftNext = leftN.next;
                  leftN.next = rightCur;
                  rightCur = leftN;
                  leftN = leftNext;
              }
              head = rightN;
          }
          return head;
      }
  }
